package com.restaurant.springboot.repositories;

import com.restaurant.springboot.entity.CouponEntity;
import com.restaurant.springboot.entity.CouponEntityPK;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.beans.Transient;
import java.util.List;
import java.util.Map;

public interface CouponRepository extends JpaRepository<CouponEntity, CouponEntityPK> {

    @Transient
    @Query(value = "SELECT\n" +
            "cp.user_id,\n" +
            "cp.coupon_series_id,\n" +
            "cp.receive_time,\n" +
            "cps.effective_time,\n" +
            "cps.end_time,\n" +
            "cps.mix_limit,\n" +
            "cps.`name`,\n" +
            "cps.quota,\n" +
            "cps.start_time\n" +
            "FROM\n" +
            "coupon_series AS cps\n" +
            "INNER JOIN coupon AS cp ON cps.id = cp.coupon_series_id\n" +
            "where DATEDIFF(cps.end_time,NOW()) > 0 AND DATEDIFF(cps.start_time,NOW()) <= 0 AND " +
            "cp.user_id=:userId AND used=0\n",
            nativeQuery = true)
    List<Map<String,Object>> findAllCouponByUserId(@Param("userId") int userId);

    @Transient
    void deleteByUserIdAndCouponSeriesId(int userId,int couponSeriesId);

    boolean existsByUserIdAndCouponSeriesId(int userId,int couponSeriesId);

    CouponEntity findCouponEntityByUserIdAndCouponSeriesId(int userId,int couponSeriesId);



}